<!-- 饼图 -->
<template>
  <div>
    <!-- <div class="title">
      <p>{{ title }}</p>
    </div> -->
    <div ref="piechart" :style="{ height: height, width: width }"></div>
  </div>
</template>

<script>
require("echarts/theme/macarons"); // echarts theme
export default {
  data() {
    return {
      color: ""
    };
  },
  props: {
    title: {
      type: String,
      default: "综合统计数据分析"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    tooltip: {
      type: String,
      default: "数量"
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.color = localStorage.getItem("isNightMode") || "night";
    this.$nextTick(() => {
      this.setOptions(this.chartData);
    });
  },
  methods: {
    setOptions(chartData) {
      let that = this;
      var showData = [];
      var sum = 0,
        max = 0;
      chartData.forEach((item) => {
        sum += item.value;
        if (item.value >= max) max = item.value;
      });
      // 放大规则
      var number = Math.round(max * 0.1);
      showData = chartData.map((item) => {
        return {
          value: number + item.value,
          name: item.name,
          id: item.id
        };
      });
      this.myChart = this.$echarts.init(this.$refs.piechart);// 设置设备像素比
      this.myChart.setOption({
        title: {
          left: "center"
        },
        tooltip: {
          trigger: "item",
          formatter: function (param) {
            return `${param.name} <br/>${that.tooltip} :  ${
              param.value - number
            }`;
          }
        },
        legend: {
          type: "scroll",
          orient: "vertical",
          right: 15,
          top: 20,
          bottom: 20,
          width: 70,
          textStyle: {
            color: this.color === 'night' ?"#ccc":"#000" 
          },
          formatter: function (name) {
            //通过name获取到数组对象中的单个对象
            let singleData = chartData.filter(function (item) {
              return item.name == name;
            });
            return `${name} ( ${singleData[0].value} )`;
          }
        },
        series: [
          {
            type: "pie",
            roseType: "radius",
            radius: [20, 120],
            data: showData,
            label: {
              normal: {
                show: true,
                textStyle: {
                  color: this.color === 'night' ?"#ccc" : "#000"
                },
                formatter: function (param) {
                  return param.name + " (" + (param.value - number) + ")";
                }
              }
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0
              }
            }
          }
        ]
      });
      this.myChart.off("click");
      this.myChart.on("click", (params) => {
        this.$emit("pieclick", params.name, this.title, params.data.id);
      });
    }
  }
};
</script>
<style scoped>
</style>